
class Tools{
    constructor(){}

   

    //函数防抖
    debounceWithTrigger( fn , layout , trigger ){
        let time_id = null;
        return function( ){
            let that = this;
            
            if( time_id ){
                clearTimeout( time_id );
            }
            let first = !time_id;
            if( trigger ){

                if( first ){
                    fn.apply( that ,  that.arguments );
                }

                time_id = setTimeout(function(){
                    time_id = null;
                }, layout);
                
            }else{
                console.log(that.arguments);
                time_id = setTimeout( function(){
                    fn.apply( that ,  that.arguments );
                }, layout);
            }
        }
    }

    //函数防抖 for vue带有事件参数
    debounceWithTrigger_events( fn , layout , trigger ){
        let time_id = null;
        return function( e ){
            let that = this;
            
            if( time_id ){
                clearTimeout( time_id );
            }
            let first = !time_id;
            if( trigger ){

                if( first ){
                    
                    fn.call( that , e );
                }

                time_id = setTimeout(function(){
                    time_id = null;
                }, layout);
                
            }else{
                console.log(that.arguments);
                time_id = setTimeout( function(){
                    
                    fn.call( that , e);
                }, layout);
            }
        }
    }

    // 节流 
    throttle( fn , timeout ){
        let begin = Date.now();
        
        return function(){
            let now = Date.now();
            if( now - begin > timeout ){
                
                fn().apply(this , this.arguments);
                begin = now;
            }

        }
    }
    
}

//单例处理过程
let toolObj = function(){
    
    if( !window.$tools ){
        window.$tools = new Tools();
    }

    return window.$tools;
}

exports.tools = toolObj();
